package com.helper;


import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import android.content.Context;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class BdHelper extends SQLiteOpenHelper {
	
	//
	private static  Context myContext;
	private SQLiteDatabase myDataBase;
	private final String DB_PATH;
	private static String DB_NAME = "mellariusBd.db3";
	
	/**
	 * Constructor Takes and keeps a reference of the passed context in order to
	 * access to the application assets and resources.
	 * 
	 * @param context
	 * @throws IOException
	 */

	public BdHelper(Context context) throws IOException {
		
		super(context,DB_NAME, null, 1);
		this.myContext = context;
		DB_PATH = this.getWritableDatabase().getPath();	
	
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
	    // TODO Auto-generated method stub

	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
	    // TODO Auto-generated method stub

	}

	/**
	 * This method will create database in application package /databases
	 * directory when first time application launched
	 **/
	public void createDataBase() throws IOException {
		
	    boolean mDataBaseExist = checkDataBase();
	    if (!mDataBaseExist) {
	        this.getWritableDatabase();
	        try {
	            copyDataBase();
	        } catch (IOException mIOException) {
	            mIOException.printStackTrace();
	            throw new Error("Error copying database");
	        } finally {
	            this.close();
	        }
	    }
	    
	}

	/** This method checks whether database is exists or not **/
	public static  boolean checkDataBase(Context context) {
		myContext = context;
        String destPath = myContext.getFilesDir().getPath();
        destPath = destPath.substring(0, destPath.lastIndexOf("/")) + "/databases/"+DB_NAME;
        File file = new File(destPath);
        boolean result = false;
        if(file.exists()&&(file.length()==12288||file.length()==3072||file.length()==0)) {
        	deleteDataBase();
        	result = false;
        }
        if(file.exists()&&file.length()!=12288&&file.length()!=3072) {
        	result = true;
        }
        return result;
	}
	public static  boolean checkDataBase() {
        String destPath = myContext.getFilesDir().getPath();
        destPath = destPath.substring(0, destPath.lastIndexOf("/")) + "/databases/"+DB_NAME;
        File file = new File(destPath);
        if(file.exists()) {
             return true;
        }else{
        	return false;
        }  
	    
	}

	/**
	 * This method will copy database from /assets directory to applicati on
	 * package /databases directory
	 **/
	public  void copyDataBase() throws IOException {
	    try {

	        InputStream mInputStream = myContext.getAssets().open(DB_NAME);
	        OutputStream mOutputStream = new FileOutputStream(myContext.getDatabasePath(DB_NAME));
	        byte[] buffer = new byte[1024];
	        int length;
	        while ((length = mInputStream.read(buffer)) > 0) {
	            mOutputStream.write(buffer, 0, length);
	        }
	        
	        mOutputStream.flush();
	        mOutputStream.close();
	        mInputStream.close();
	    } catch (Exception e) {
	        e.printStackTrace();
	    }
	    copyLastIp();
	}
	public  void copyLastIp() throws IOException {
	    try {

	        InputStream mInputStream = myContext.getAssets().open("lastIp.txt");
	        OutputStream mOutputStream = new FileOutputStream(myContext.getDatabasePath("lastIp.txt"));
	        byte[] buffer = new byte[1024];
	        int length;
	        while ((length = mInputStream.read(buffer)) > 0) {
	            mOutputStream.write(buffer, 0, length);
	        } 
	        mOutputStream.flush();
	        mOutputStream.close();
	        mInputStream.close();
	    } catch (Exception e) {
	        e.printStackTrace();
	    }
	}

	/** This method open database for operations **/
	public SQLiteDatabase openDataBase() throws SQLException {

	    myDataBase = SQLiteDatabase.openDatabase(DB_PATH, null,
	            SQLiteDatabase.OPEN_READWRITE);
	    return myDataBase;
	    
	}
	public static void deleteDataBase() throws SQLException {
		String destPath = myContext.getFilesDir().getPath();
        destPath = destPath.substring(0, destPath.lastIndexOf("/")) + "/databases/"+DB_NAME;
        File file = new File(destPath);
        if(file.exists()) {
        	file.delete();
        }
	    
	}

	/** This method close database connection and released occupied memory **/
	@Override
	public synchronized void close() {
		
	    if (myDataBase != null)
	        myDataBase.close();
	    SQLiteDatabase.releaseMemory();
	    super.close();
	    
	}

}